package jfood.parser;

import java.io.IOException;
import java.util.ArrayList;

import jfood.ontology.meal.Ingredient;
import jfood.ontology.meal.Meal;
import jfood.ontology.meal.Recipe;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class CoJemyParser implements IParser {

	private static final String PAGE_URL = "http://www.cojemy.pl/szukaj.php?s=a&x=69&y=21&pnr=";
//	private static final String PAGE_MEAL_URL = "http://www.cojemy.pl/przepis-na/";

	private String getName(Element element) {
		String name = element.html();

		name = name.split("strong")[1];
		name = name.replaceAll("[>,<,//]", "");
		name = name.replaceAll("&oacute;", "ó");		
//		System.out.println(name);
		return name;
	}

	private ArrayList<Ingredient> getIngredients(Element skladniki) {
		String ingredientsHtml = skladniki.html().replace("<p>", "");
		ingredientsHtml = ingredientsHtml.replace("&oacute;", "ó");
		ingredientsHtml = ingredientsHtml.replaceAll("<strong>.*</strong>", "");
		ingredientsHtml = ingredientsHtml.replaceAll("&ccedil;", "c");
		ingredientsHtml = ingredientsHtml.replaceAll("&nbsp;", "\n");
		
//		ingredientsHtml = AlphabetNormalizer.unAccent(ingredientsHtml);
		ArrayList<Ingredient> ingredients = new ArrayList<Ingredient>();
		
		for (String ingredientText : ingredientsHtml.split("</p>|<br />"))
		{
			ingredientText = ingredientText.replaceAll("<.*>", "");
			ingredientText = ingredientText.replaceAll("^\\s*", "");
			if(ingredientText.isEmpty() == false)
			{
				ingredientText = ingredientText.replaceAll("[\r\n]","");
				Ingredient ingredient = new Ingredient(ingredientText);
				ingredients.add(ingredient);
			}
		}

		return ingredients;
	}

	private Recipe getReceipe(Element opis) {
		String receipeHtml = opis.html();
		String receipe = receipeHtml.replaceAll("<p>|<br />|</p>", "");
//		receipe = receipe.replace("</p>", "");
		receipe = receipe.replace("&oacute;", "ó");
		receipe = receipe.replaceAll("[\r\n]","");
//		receipe = AlphabetNormalizer.unAccent(receipe)
//		receipe = receipe.replaceAll("<.*>", "");
		receipe = receipe.replace("&nbsp;", " ");
		receipe = receipe.replace("&quot;", "\"");
		receipe = receipe.replaceAll("<strong>", "");
		receipe = receipe.replaceAll("</strong>", "");
		
		return new Recipe(receipe);
	}

	public Meal parsePage(String url) {
		try {
			Document doc = Jsoup.connect(url).get();

			Element mealName = doc.getElementsByClass("recipe-title").first();
			
			Element ingredients = doc.getElementsByClass("potrawa-skladniki")
					.first();

			Element recipe = doc.getElementsByClass("potrawa-opis").first();
			recipe = recipe.children().first();

			return new Meal(getName(mealName), getIngredients(ingredients),
					getReceipe(recipe));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public ArrayList<Meal> parse() {
		try {
			ArrayList<Meal> meals = new ArrayList<Meal>();
			boolean continueLooking = true;
			int i = 1;
			while (continueLooking) {
				continueLooking = false;
				Document doc = Jsoup.connect(PAGE_URL + i++).get();
				Elements receipes = doc.getElementsByClass("recipe-listing");
				for (Element el : receipes) {
					Element link = el.getElementsByTag("a").first();
					meals.add(parsePage(link.attr("href")));
				}

				Element navigation = doc
						.getElementsByClass("pagingContainerUp").first();
				for (Element page : navigation.children()) {
					if (!page.className().equals("pagingNumber")) {
						continue;
					}
					Element pageLink = page.getElementsByTag("a").first();
					String data = pageLink.html();
					if (data.equals("&raquo;")) {
						continueLooking = true;
						break;
					}
				}
			}
			return meals;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		CoJemyParser parser = new CoJemyParser();

		ArrayList<Meal> meals = parser.parse();

		int i = 0;
		for (Meal meal : meals) {
			System.out.println(meal);
			System.out.println(i++);
		}
//		String asd = "<p style=\"padding-top: 10px;\">";
//		System.out.println(asd.replaceAll("<.*>","a"));
		
	}
}
